#!/bin/bash
#author          :Ricardo de Souza Maia
#license         :GPLv3
#version         :$Id
#encoding        :UTF-8
#Target          :GNU/Linux CentOS

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#                          CONFIGURAÇÃO DE VARIÁVEIS                          #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

#Diretorio Web Raiz
APACHE_WWW="/mnt/portaldata/var/www/html"

#Pasta raiz da configuracoes do Apache
APACHE_ROOT="/etc/httpd"

#Configuracoes do Apache
APACHE_CONF="$APACHE_ROOT/conf/httpd.conf"

#Pasta raiz da configuracoes do Apache
PHP_SESSIONS="/mnt/portaldata/var/lib/php/session"

#--------------------- Não editar abaixo desta linha -------------------------

###############################################################################
# Testa se as pastas definidas existem
###############################################################################


if [ ! -d ${APACHE_WWW} ]; then
  echo "Erro: O diretório ${APACHE_WWW} não existe."
  exit 1
fi

if [ ! -d ${APACHE_ROOT} ]; then
  echo "Erro: O diretório ${APACHE_ROOT} não existe."
  exit 1
fi

if [ ! -f ${APACHE_CONF} ]; then
  echo "Erro: O arquivo ${APACHE_CONF} não existe."
  exit 1
fi

CURRENT_DIR=$(cd $(dirname "$0"); pwd)
source "${CURRENT_DIR}/global_functions.sh"

clear
show_banner

###############################################################################
# Criação de novo usuário. Será utilizado pelo serviço FTP e quotas.
###############################################################################
while true
  read -p "Informe o nome de usuário: " USERNAME
  user_exists $USERNAME

 do
   if [ $? -eq 0 ]; then
    echo -e "${YELLOW}[!]${RESET} ERRO: O usuario ${RED}'$USERNAME'${RESET} já existe. Tente outro nome."
    restart_app
   else
     break
   fi;
 done

 ###############################################################################
# Define a quota de disco para o usuário criado
###############################################################################
echo "ESPACO DISPONIVEL EM DISCO"
df -h --total

read -p "Informe a quota de disco para o usuario (em MB): " SOFTQUOTA
SOFTQUOTAKB=`expr $SOFTQUOTA \\* 1024`
HARDQUOTA=`expr $SOFTQUOTA + 50`
HARDQUOTAKB=`expr $HARDQUOTA \\* 1024`

echo -e "Você definiu uma quota de ${RED}'$SOFTQUOTA MB'${RESET} ($SOFTQUOTAKB KB) para o usuário ${RED}'$USERNAME'${RESET}"
echo "O usuário não terá permissão de escrita ao alcançar $HARDQUOTA MB ($HARDQUOTAKB KB) em disco."

###############################################################################
# Endereço do novo WebSite.
###############################################################################
read -p "Informe o nome do website (www.example.com): " WEBSITE_NAME

###############################################################################
# Executando as operações configuradas
###############################################################################

echo "
<VirtualHost *:80>

php_admin_value open_basedir      \"${APACHE_WWW}/${USERNAME}:/tmp:/usr/share/pear:/usr/share/php\"
php_admin_value upload_tmp_dir    \"${APACHE_WWW}/${USERNAME}/tmp\"
php_admin_value session.save_path \"${PHP_SESSIONS}/${USERNAME}\"

DocumentRoot \"${APACHE_WWW}/${USERNAME}/htdocs\"
ServerName ${WEBSITE_NAME}
ErrorLog /var/log/httpd/error-${USERNAME}.log
LogLevel warn
CustomLog /var/log/httpd/access-${USERNAME}.log combined

        <Directory \"${APACHE_WWW}/${USERNAME}/htdocs\">
          Order allow,deny
          Allow from all
          Options None
          AllowOverride Options FileInfo
        </Directory>

</VirtualHost>"

# Append no arquivo de configuracao do Apache

echo "${APACHE_WWW}/${USERNAME}/{htdocs,tmp,sessions}"
#mkdir -p echo ${APACHE_WWW}/${USERNAME}/{htdocs,tmp,sessions}

echo "${PHP_SESSIONS}/${USERNAME}"
#mkdir ${PHP_SESSIONS}/${USERNAME}

echo "/etc/init.d/httpd restart"
#/etc/init.d/httpd restart

